package com.darren.euc.action;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.darren.euc.entity.Order;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.User;
import com.darren.euc.service.OrderService;
import com.darren.euc.util.PropertyHelper;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/order")
public class OrderAction {
	
	private static final Logger log = Logger.getLogger(OrderAction.class);
	
	private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd");
	
	@Autowired
    private OrderService orderService;
	
	@ResponseBody
    @RequestMapping(value = "/list")//,method = RequestMethod.POST)
    public List<Order> getAll(HttpServletRequest request,HttpServletResponse response) {
    	List<Order> orders = orderService.getAll();    	
        return orders;
    }
	
	@ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public ResponseJSON queryOrder(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = orderService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/queryseatorder",method = RequestMethod.POST)
    public ResponseJSON querySeatOrderList(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = orderService.querySeatOrderForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseJSON saveOrder(@RequestBody Order order,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if(user != null){	
				order.setCreateUserId(user.getId());
				order.setLastModifyUserId(user.getId());
			}
			order.setCreateTime(new Date());
			order.setLastModifyTime(new Date());
			order = orderService.save(order);
			order.setOrderNumber(generateOrderNumber(orderService.getCurrentDayCount(order.getId())));
			order = orderService.save(order);
			responseJson.setStatus(0);
			responseJson.setMessage("添加成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("添加失败！");
		}
		responseJson.setData(order);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
    }
	
	private String generateOrderNumber(BigInteger orderNumber){
		Date currentTime = new Date();
		return "R" + FORMATTER.format(currentTime) + String.format("%03d", orderNumber);
	}
	
	@ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseJSON deleteRole(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
    	ResponseJSON responseJson = new ResponseJSON();
    	JSONObject jsonData = new JSONObject();
    	responseJson.setData(jsonData);
    	try{
    		Integer id = Integer.parseInt(requestJson.getString("id"));    		
    		Order order = new Order();
    		order.setId(id);
    		orderService.delete(order);
        	responseJson.setStatus(0);
    		responseJson.setMessage("删除成功！");
    	}catch(Exception ex){
    		log.info(ex.getMessage(),ex);
        	responseJson.setStatus(-1);
    		responseJson.setMessage("删除失败！");
    	}
    	System.out.println("==============================================================");
    	System.out.println(requestJson.toString());
    	System.out.println(responseJson.toString());
    	System.out.println("==============================================================");
        return responseJson;
    }
	  
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseJSON updateOrder(@RequestBody Order order, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {				
			User user = (User) request.getAttribute("user");
			if(user != null){				
				order.setLastModifyUserId(user.getId());
			}
			order.setLastModifyTime(new Date());
			order = orderService.update(order);			
			responseJson.setStatus(0);
			responseJson.setMessage("保存成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(order);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
}